<?php

//物业费
namespace App\Http\Controllers\Property;

use App\Common\json;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class StratafeeController extends Controller{

    public function Sum($id){
        $data    =DB::table('strata_fee')
            ->where('user_stratafee',$id)
            ->sum('money');
        return $data;
    }


    public function SelectStratafee(Request $request){
        if($request ->isMethod('post')){
            $user_id      = $request ->input('user_id');
            $data         = DB::table('strata_fee')
                ->leftjoin('user','strata_fee.user_stratafee','=','user.user_id')
                ->where('user_stratafee',$user_id)
                ->where('state',0)
                ->get();
            foreach ($data as $value){
                $id             = $value ->years;
               // $sum            = $this->Sum($id);
               // $tmpArray[$id]['sum'] =$sum;
                $tmpArray[$id]['years'] =$value->years;
                $tmpArray[$id]['money'] =$value->money;
                $da = array_values($tmpArray);
            }
            if($data == true){
                return json::json('success', '获取物业费信息', $da);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',[]);
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }
    //已经完成交费
    public function AllStratafee(Request $request){
        if($request ->isMethod('post')){
            $user_id      = $request ->input('user_id');
            $data         = DB::table('strata_fee')
                 ->leftjoin('user','strata_fee.user_stratafee','=','user.user_id')
                ->where('user_stratafee',$user_id)
                ->where('state',1)
                ->get();
             foreach ($data as $value){
                 $id             = $value ->years;
                 // $sum            = $this->Sum($id);
                 // $tmpArray[$id]['sum'] =$sum;
                 $tmpArray[$id]['years'] =$value->years;
                 $tmpArray[$id]['money'] =$value->money;
                 $da = array_values($tmpArray);
             }
            if($data == true){
                return json::json('success', '获取物业费信息', $da);
            }else{
                $arr  = (object)array();
                return json::json('failure','由于网络或者不正当原因失败',$arr);
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }
}